package com.bobo.web.provider.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Collections;

@Component
public class JedisUtils {
    @Autowired
    private JedisPool jedisPool;
    private static final String LOCK_SUCCESS = "OK";
    private static final String SET_IF_NOT_EXIST = "NX";
    private static final String SET_WITH_EXPIRE_TIME = "PX";
    private static final Long RELEASE_SUCCESS = 1L;

    /**
     * 获取锁
     * @param lockKey
     * @param requestId
     * @param expireTime EX = seconds; PX = milliseconds
     * @return
     */
    public boolean tryGetDistributedLock(String lockKey, String requestId, Long expireTime){
        // 使用一条命令执行NX EX操作，保证原子性
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
            if (LOCK_SUCCESS.equals(result)) {
                return true;
            }
        }finally {
            jedis.close();
        }
        return false;
    }

    /**
     * 释放锁
     * @param jedis Redis客户端
     * @param lockKey 锁
     * @param requestId 请求标识
     * @return 是否释放成功
     */
    public boolean releaseDistributedLock(Jedis jedis, String lockKey, String requestId) {
        try{
            jedis = jedisPool.getResource();
            //使用脚本保证，GET 判断 DEL 是原子性的
            String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
            Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
            if (RELEASE_SUCCESS.equals(result)) {
                return true;
            }
        }finally {
            jedis.close();
        }
        return false;

    }
    /**
     * 释放锁
     * @param key 锁
     * @param value 请求标识
     * @return 是否释放成功
     */
    public boolean setKey(String key, String value) {
        Jedis jedis =null;
        try{
            jedis = jedisPool.getResource();
            String result = jedis.set(key, value, SET_IF_NOT_EXIST);
            if (RELEASE_SUCCESS.equals(result)) {
                return true;
            }
        }finally {
            jedis.close();
        }
        return false;

    }
    /**
     * hash-set
     * @param key 锁
     * @param value 请求标识
     * @return 是否释放成功
     */
    public boolean hset(String key, String field,String value) {
        Jedis jedis =null;
        try{
            jedis = jedisPool.getResource();
            jedis.hset(key,field,value);
        }finally {
            jedis.close();
        }
        return false;

    }
    /**
     * hash-set
     * @param key 锁
     * @return 是否释放成功
     */
    public String hget(String key, String field) {
        Jedis jedis =null;
        try{
            jedis = jedisPool.getResource();
            return jedis.hget(key, field);
        }finally {
            jedis.close();
        }
    }
    /**
     * 释放锁
     * @param key 锁
     * @param value 请求标识
     * @return 是否释放成功
     */
    public boolean lPush(String key, String[] value) {
        Jedis jedis =null;
        try{
            jedis = jedisPool.getResource();
            jedis.lpush(key,value);
        }finally {
            jedis.close();
        }
        return false;

    }
    /**
     * 获取列表的长度
     * @param key 锁
     * @return 是否释放成功
     */
    public Long getLenth(String key) {
        Jedis jedis =null;
        try{
            jedis = jedisPool.getResource();
            return jedis.llen(key);
        }finally {
            jedis.close();
        }
    }
    /**
     * 弹出列表头数据
     * @param key
     */
    public String lPop( String key) {
        Jedis jedis =null;
        try{
            jedis = jedisPool.getResource();
            return jedis.lpop(key);
        }finally {
            jedis.close();
        }
    }
}
